package com.dhecp.project.hr.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.utils.ExportExcelUtils;
import com.dhecp.project.api.util.ApiConfig;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.hr.domain.AmazonAnalysis;
import com.dhecp.project.hr.domain.AmazonShopProductReport;
import com.dhecp.project.hr.service.AmazonAnalysisService;
import com.dhecp.project.system.domain.SysDictionaryForExport;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * @program: kjecp
 * @description: 亚马逊商品运营分析表
 * @author: hlf
 * @create: 2022-10-13 15:58
 */
@Controller
@RequestMapping("/hr/amazonanalysis")
public class AmazonAnalysisController extends BaseModuleController {

    private String prefix = "hr/amazonanalysis";
    private String moduleTable = ModuleTable.AMAZON_ANALYSIS;
    private String moduleTable_REPORT = ModuleTable.AMAZON_SHOP_PRODUCT_REPORT;

    @Autowired
    private AmazonAnalysisService amazonAnalysisService;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    /**
     * 打开查询列表页
     * @param params
     * @return
     * @author DH-Xxj 
     */
    @RequiresPermissions("hr:amazonanalysis:view")
    @GetMapping()
    public ModelAndView listPage(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable_REPORT);
        params.put("entity", new AmazonShopProductReport()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
        params.put("prefix",prefix + "/amazonanalysis"); //页面路径
        params.put("checkBox", "yes");

        ModelAndView mv = this.openListPage(params);
        mv.addObject("hr", "amazonanalysis");
        mv.addObject("ali1688AppKey", ApiConfig.getAli1688AppKey());
        mv.addObject("ali1688EcpApiUrl", ApiConfig.getAli1688EcpApiUrl());
        Object importFields = dictionaryForFieldService.queryFieldFrontImport(params); //导入模板字段
        mv.addObject("importFields", importFields);	//导入模板字段
        return mv;
    }

    /**
     * 查询页 列表数据
     * @param params
     * @return
     * @author DH-Xxj
     */
    @RequiresPermissions("hr:amazonanalysis:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable_REPORT);
        List<AmazonAnalysis> list = new ArrayList<AmazonAnalysis>();
        try {
            list = amazonAnalysisService.queryList(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());
        }
        return getDataTable(list);
    }


    /**
     * 导出Excel导入模板
     * @param params
     * @return
     * @throws Exception
     * @author DH-Xxj
     */
    @RequiresPermissions("hr:amazonanalysis:import")
    @Log(title = "分析报告模板导出", businessType = BusinessType.EXPORT)
    @GetMapping("/exporttemplate")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult exportTemplate(@RequestParam Map<String, Object> params) throws Exception {
        params.put("moduleTable", moduleTable_REPORT);
        params.put("entity", new AmazonShopProductReport());

        params.put("getFieldAttributesType", "template");
        List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<AmazonShopProductReport> util = new ExportExcelUtils<AmazonShopProductReport>(moduleTable,new AmazonShopProductReport());
        return util.exportExcel(null, fieldList, "分析报告导入模板", "template");
    }

    /**
     * 导入并直接生成生效单据
     * @param infoItems 必要参数：对应实体类的json格式字符串（导入的字段用户内容）
     * @return
     * @throws Exception
     * @author DH-Xxj
     */
    @RequiresPermissions("hr:amazonanalysis:import")
    @Log(title = "分析报告导入", businessType = BusinessType.EXPORT)
    @PostMapping("/importdatapost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult importData(@RequestParam Map<String, Object> params) throws Exception {
        params.put("moduleTable", moduleTable_REPORT);
		/*导入测试
		String infoItems = "[{'vGwMc':'岗位1','vHdRs':'1'},{'vGwMc':'岗位2','vHdRs':'2'}]";
		params.put("infoItems", infoItems);
		*/
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= amazonAnalysisService.saveImportData(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }


}